/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ui.user.mail;

import hibernate.dao.CourseDAO;
import hibernate.entity.Accounts;
import hibernate.entity.Courses;
import hibernate.entity.Students;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import org.primefaces.event.TransferEvent;
import org.primefaces.model.DualListModel;
import util.Converter;

/**
 *
 * @author Administrator
 */
@ManagedBean
@RequestScoped
public class AssigmentComposer extends MailComposer {

    /**
     * Creates a new instance of AssigmentComposer
     */
    DualListModel<Courses> dualCourse;
    DualListModel<Accounts> dualAccount;
    List<Courses> sourceCourse, targetCourse;
    HashMap<Integer, Integer> mapStudent;
    int courseID;
    List<String> listS ;
    String[] arrS;
    List<Students> receiverList;
    HashMap<Students, Boolean> mapStudentSelect;
    HashMap<Integer, String> mapS = new HashMap<Integer, String>();
    List<Integer> listN ;

    public AssigmentComposer() {
        sourceCourse = Converter.iteratorToList(getCurrentStaff().getCourseses().iterator());
        targetCourse = new ArrayList<Courses>();
        dualCourse = new DualListModel<Courses>(sourceCourse, targetCourse);
        List<Courses> listC = Converter.iteratorToList(getCurrentStaff().getCourseses().iterator());
        mapStudentSelect = new HashMap<Students, Boolean>();
        listS = new ArrayList<String>();
        
    }

    public String[] getArrS() {
        return arrS;
    }

    public void setArrS(String[] arrS) {
        this.arrS = arrS;
    }
    
    public HashMap<Integer, String> getMapS() {
        return mapS;
    }
    
    public void setMapS(HashMap<Integer, String> mapS) {
        this.mapS = mapS;
    }

//    public HashMap<Integer, HashMap> getMapCourse() {
//        return mapCourse;
//    }
//
//    public void setMapCourse(HashMap<Integer, HashMap> mapCourse) {
//        this.mapCourse = mapCourse;
//    }
    public List<String> getListS() {
        return listS;
    }
    
    public void setListS(List<String> listS) {
        this.listS = listS;
    }
    
    public List<Students> getReceiverList() {
        
        return receiverList;
    }
    
    public void setReceiverList(List<Students> receiverList) {
        
        this.receiverList = receiverList;
    }
    
    public DualListModel<Courses> getDualCourse() {
        return dualCourse;
    }
    
    public void setDualCourse(DualListModel<Courses> dualCourse) {
        this.dualCourse = dualCourse;
    }
    
    public int getCourseID() {
        return courseID;
    }
    
    public void setCourseID(int courseID) {
        this.courseID = courseID;
    }
    
    public HashMap<Students, Boolean> getMapStudentSelect() {
        return mapStudentSelect;
    }
    
    public void setMapStudentSelect(HashMap<Students, Boolean> mapStudentSelect) {
        this.mapStudentSelect = mapStudentSelect;
    }

    public List<Integer> getListN() {
        return listN;
    }

    public void setListN(List<Integer> listN) {
        this.listN = listN;
    }

//
//    public void courseTransfer(TransferEvent event) {
//
//        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
//        receiverList = (List<Students>) session.getAttribute("receiverList");
//        mapCourseID = (HashMap<Integer, Integer>) session.getAttribute("mapCourseID");
//        if (receiverList == null) {
//            receiverList = new ArrayList<Students>();
//        }
//        if (mapCourseID == null) {
//            mapCourseID = new HashMap<Integer, Integer>();
//        }
//        courseID = Integer.parseInt(event.getItems().get(0).toString());
//        mapCourseID.put(courseID,courseID);
//        Courses cs = new CourseDAO().readByID(courseID);
//        if (event.isAdd()) {
////            receiverList.addAll(cs.getStudentses());
//        } else if (event.isRemove()) {
////            receiverList.removeAll(cs.getStudentses());
//            for (Students stu : receiverList) {
//                for (Students s1 : cs.getStudentses()) {
//                    if (s1.getStudentId() == stu.getStudentId()) {
//                        receiverList.remove(stu);
//                    }
//                }
//            }
//        }
//        for (Courses c : dualCourse.getTarget()) {
//            Iterator<Students> stuOfCourse = c.getStudentses().iterator();
//            while (stuOfCourse.hasNext()) {
//                Students st = stuOfCourse.next();
//                receiverList.add(st);
//            }
//        }
//        session.setAttribute("receiverList", receiverList);
//        session.setAttribute("mapCourseID", mapCourseID);
//        sourceAccount = listA;
//        for (Accounts accounts : dualAccount.getTarget()) {
//            if (listA.contains(accounts)) {
//                dualAccount.getTarget().remove(accounts);
//            }
//        }
//        dualAccount = new DualListModel<Accounts>(sourceAccount, targetAccount);
//    }
    public void importStudent() {
        
//        Iterator it = mapStudentSelect.entrySet().iterator();
//        for (int id : listN) {
//            receiverString.concat(";"+id);
//        }
        for (String id : listS) {
            receiverString.concat(";"+id);
        }
    }
    
    public void selectCourse() {
        if (courseID != 0) {
            Courses c = new CourseDAO().readByID(courseID);
            Iterator<Students> it = c.getStudentses().iterator();
            receiverList = Converter.iteratorToList(it);
            getHttpSession().setAttribute("receiverList", receiverList);
//            while (it.hasNext()) {
            for (Students st : receiverList) {
                mapS.put(st.getAccounts().getAccountId(), st.getAccounts().getFullName());
                mapStudentSelect.put(st, Boolean.FALSE);
            }
            
        }

//        listAccount = Converter.iteratorToList(it);

    }
}